<?php

namespace App\Imports;

use App\Models\Qh_Course;
use App\Models\Qh_Knowledge;
use App\Models\Qh_Question;
use App\Models\Qh_QuestionLibrary;
use App\Models\Qh_QuestionType;
use DB;
use Illuminate\Support\Collection;
use Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;

class QhQuestionImport implements ToCollection
{
    protected array $params;
    public function __construct($params)
    {
        $this->params = $params;
    }

    public function collection(Collection $collection)
    {
        //如果需要去除表头
        unset($collection[0]);
        //$rows 是数组格式
        $this->createData($collection);
    }

    public function createData($collection)
    {
        DB::transaction(function () use ($collection) {
            $questionIds = [];

            foreach ($collection as $item) {
                // 题型识别
                $questionType = Qh_QuestionType::where('name', $item[1])->first();
                if (!$questionType) {
                    $questionType = Qh_QuestionType::create([
                        'name' => $item[1],
                        'created_at' => time(),
                    ]);
                }

                // 选项数量
                $optionItemCount = count($item) - 4;
                $options = [];
                for ($i=2; $i<$optionItemCount+2; $i++) {
                    if (empty($item[$i]) && $item[$i] != "0") continue;
                    $options[] = $item[$i];
                }
                $optionCount = count($options);

                $optionStr = $this->formatOptions($options);

                $questionData = [
                    'qlId' => $this->params['qlId'],
                    'courseId' => $this->params['courseId'],
                    'knowledgeId' => $this->params['knowledgeId'],
                    'questionTypeId' => $questionType->id,
                    'title' => $item[0],
                    'options' => $optionStr,
                    'optionCount' => $optionCount,
                    'answer' => $item[$optionItemCount+2],
                    'analysis' => $item[$optionItemCount+3],
                    'area' => !empty($this->params['area']) ? $this->params['area'] : null,
                    'questionFrom' => 'import',
                    'difficulty' => 1,
                    'difficultyValue' => 0.5,
                    'created_at' => time(),
                ];
                // 入库
                $question = Qh_Question::create($questionData);
                $questionIds[] = $question->id;
            }

            // 试题数量更新
            updateQhQuestionCount($this->params['knowledgeId'], count($questionIds));

            Log::info('EXCEL导入('.count($questionIds).'道)', $questionIds);
        });
    }

    /**
     * 组装options
     * @param $options
     * @return string
     */
    private function formatOptions($options): string
    {
        $optionKeys = range('A', 'Z');
        $optionListStr = '';
        foreach ($options as $key => $option) {
            $optionListStr.=$optionKeys[$key].'、'.$option;
            if ($key < count($options)-1) $optionListStr.='|NTK|';
        }

        return $optionListStr;
    }
}
